import { defineStore } from 'pinia'

// 匹配views里面所有的.vue文件
const modules = import.meta.glob('./../../views/**/*.vue')

const usePermissionStore = defineStore('permission', {
  state: () => ({
    routes: [], // 总路由集合
    sidaberRoutes: [], // 侧边栏路由列表
    sidaberRouteName: '', // 侧便当前路由名
    topRoutes: [] // 顶部路由集合
  }),
  actions: {
    setRoutes(routes: any) {
      this.routes = routes
    },
    setSidaberRoutes(sidaberRoutes: any) {
      this.sidaberRoutes = sidaberRoutes
    },
    setSidaberRouteName(name: string) {
      this.sidaberRouteName = name
    },
    setTopRoutes(topRoutes: any) {
      this.topRoutes = topRoutes
    },
    generateRoutes() {
      return new Promise(() => {
        // 向后端请求路由数据
        // getUserMenu().then((res: any) => {
        //   console.log(res)
        // })
        // resolve()
      })
    }
  }
})

export const loadView = (view: any) => {
  let res
  for (const path in modules) {
    const dir = path.split('views/')[1].split('.vue')[0]
    if (dir === view) {
      res = () => modules[path]()
    }
  }
  return res
}

export default usePermissionStore
